;; The first three lines of this file were inserted by DrScheme. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(planet plai/plai:1:3/lang/reader)

(define precisao 0.001)

(define (raizq x) 
  
  (define (perto? chute)
    (< (abs (- (* chute chute) x)) precisao))
  
  (define (melhorar chute)
    (/ (+ chute (/ x chute)) 2))
  
  (define (estimar chute)
    (if (perto? chute) 
        chute
        (estimar (melhorar chute))))
  (estimar 1))

;;;;;;;;;;;;;;; testes

(test-inexact-epsilon precisao)
(print-only-errors #t)

(define (proximo? a)
  (> precisao (abs (- 1 (/ (sqrt a) (raizq a))))))
 
(test (raizq 36) 6.0)
(test (raizq 1) 1)
(test (/ (raizq 3) 2) (sin (/ pi 3)))
; On-Line Encyclopedia of Integer Sequences
; http://www.research.att.com/~njas/sequences/A002193
(test (raizq 2) 1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157) 
(test/pred 6.0 proximo?)
(test/pred 1.0 proximo?)
(test/pred 2.0 proximo?)
(test/pred (* 10.0 precisao) proximo?)
(test/pred (* 2.0 precisao) proximo?)
(test/pred precisao proximo?)
(test/pred 1e48 proximo?)
;(test/pred 1e49 proximo?) ;; recursão infinita

(include "test-summary.scm")